{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "782c1968",
   "metadata": {},
   "source": [
    "------\n",
    "* 2023-10-30\n",
    "* week07笔记\n",
    "* python课程\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6ac4f2e",
   "metadata": {},
   "source": [
    "# 1.列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05817081",
   "metadata": {},
   "source": [
    "* 思考：如果把我们班同学的名字都存起来，你会怎么做？用变量name1,name2,name3....来存吗？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7de5745",
   "metadata": {},
   "source": [
    "### 1.1 体验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "062169c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "name1 = \"张三\"\n",
    "name2 = \"李四\"\n",
    "name3 = \"王五\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ee63763e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bef4174f",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"张三，李四，王五\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "593b6fd3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三，李四，王五'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ce3215d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7ae07032",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'，李四'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[2:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2b26f498",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[0:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef23a021",
   "metadata": {},
   "source": [
    "* 综述，一种新的数据结构就因此而生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "76823577",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list = [\"张三\",\"李四\",\"王五\",\"李小鹏\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d16c02ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d3602fe7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李小鹏'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "221a57d0",
   "metadata": {},
   "outputs": [],
   "source": [
    " name_学号 = [20030101001,20030101002,20030101003,20030101004]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "06da2413",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20030101001, 20030101002, 20030101003, 20030101004]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_学号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3b9293a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list[1] = \"李树人\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0618bd26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李树人', '王五', '李小鹏']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7d525f22",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_张三 = [\"张三\",20230101001,19,180,75,\"Jack\",[\"Python\",\"移动互联网\",\"UI设计\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fb5143c",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "56659e3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 20230101001, 19, 180, 75, 'Jack', ['Python', '移动互联网', 'UI设计']]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_张三"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cd4ca4d",
   "metadata": {},
   "source": [
    "* 虽然列表可以存放不同类型的数据，但是我们一般情况我们存放的数据都是同一种数据类型的\n",
    "* 官网包括：Lists might contain items of different types,but usually the items all have the same type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4b4cffa3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 允许重复的数据**（数据重复性）**\n",
    "网三_Python成绩 = [95,96,69,94,95]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f39fbb90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[95, 96, 69, 94, 95]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_Python成绩"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65b470b6",
   "metadata": {},
   "source": [
    "### 1.2创建列表\n",
    "\n",
    "> 1. 使用list()创建列表\n",
    "> 2. 直接使用[]创建列表，中间的元素用**英文**的来隔开\n",
    "> 3. 创建一个空列表，方法也是上面两种"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "33c04700",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[18, 19, 18, 20]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 使用list()创建列表\n",
    "my_list1 = list((18,19,18,20))\n",
    "print(my_list1)\n",
    "print(type(my_list1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "19645cb6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[75, 80, 74, 74]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 直接使用[]创建列表，中间的元素用英文的来隔开\n",
    "my_list2 = [75,80,74,74]\n",
    "print(my_list2)\n",
    "print(type(my_list2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "399c86a3",
   "metadata": {},
   "source": [
    "* 特殊小案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d79a9a10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['p', 'y', 't', 'h', 'o', 'n']\n"
     ]
    }
   ],
   "source": [
    "my_list3 = list(\"python\")\n",
    "print(my_list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b4ee65c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['h', 'e', 'l', 'l', 'o', ' ', 'l', 'i', 's', 't']\n"
     ]
    }
   ],
   "source": [
    "my_list4 = list(\"hello list\")\n",
    "print(my_list4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "52c2eb50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 3.创建空列表[两种方法]\n",
    "empty_list = list()\n",
    "print(empty_list)\n",
    "print(type(empty_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fd68601a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 3.创建空列表\n",
    "empty_list = []\n",
    "print(empty_list)\n",
    "print(type(empty_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed45c91f",
   "metadata": {},
   "source": [
    "### 1.3 查看列表的长度：len()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "13c6ca70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(my_list4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "545507cd",
   "metadata": {},
   "source": [
    "### 1.4 列表的取值和切片\n",
    "\n",
    "* index:取值\n",
    "* slice:切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3958af9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['p', 'y', 't', 'h', 'o', 'n']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list = list(\"python\")\n",
    "python_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "81c58615",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "b57c6653",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[4:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c96cef22",
   "metadata": {},
   "source": [
    "### 1.5课本小实验：让列表工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d3cc6536",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# 定义一个列表\n",
    "vowels = ['a','e','i','o','u']\n",
    "# 创建一个需要检查的单词\n",
    "word = \"milliwanys\"\n",
    "# 检查一个对象是否在另一个对象里面用“in”\n",
    "for letter in word: #赋予letter=word\n",
    "    if letter in vowels: #letter如果存在vowels,共同存在的元素\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3df92403",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# 快速确定有什么元音且不重复\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = \"milliwanys\"\n",
    "found = [] #创建一个空集来存入对象\n",
    "for letter in word: #赋予letter=word\n",
    "    if letter in vowels: #如果letter也存在于vowels\n",
    "        if letter not in found: #如果letter不存在于空集里面\n",
    "            found.append(letter) #空集要增加letter，即添加入相同的元素【存在于vowels和word】\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e07b1c43",
   "metadata": {},
   "source": [
    "### 1.6列表的一般方法\n",
    "\n",
    "* 笔记：数据如何处理是数据诞生之后非常重要的事情\n",
    "\n",
    "* 数据的基本操作流程：\n",
    "> 1. 增加 add\n",
    "> 2. 减少/删除 remove\n",
    "> 3. 查询 find\n",
    "> 4. 修改 update\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91b45378",
   "metadata": {},
   "source": [
    "——————\n",
    "#### 1.1.1增加append"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "68a0b65a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '王五', '李小鹏']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"张三\",\"李四\",\"王五\",\"李小鹏\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "0f913a9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 增加\n",
    "help(name_list.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "8d3e6290",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"李树人\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9b7f8a71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '王五', '李小鹏', '李树人']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8feabaa5",
   "metadata": {},
   "source": [
    "##### append和extend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "51e00757",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list = [2,3,4]\n",
    "list.extend([])\n",
    "list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8f6646bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, []]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list = [2,3,4]\n",
    "list.append([])\n",
    "list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f0000a1",
   "metadata": {},
   "source": [
    "#### 1.1.2插入insert"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1cd326f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "b4564e73",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.insert(2,\"Marry\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "aadf8a98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', 'Marry', '王五', '李小鹏', '李树人']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e028c79",
   "metadata": {},
   "source": [
    "#### 1.1.3  删除pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "966ba0e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "5f5f73d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李树人'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "c291c87f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', 'Marry', '王五', '李小鹏']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cade0a8b",
   "metadata": {},
   "source": [
    "* pop()的第二种使用方法：指定某一位（index）删除\n",
    "* 理论上：只要让我知道你的位置，我就可以pop()掉你！\n",
    "> 1. 只要满足某种条件的位置，我们就可以用pop()删掉"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "40fc70cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李四'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "960dfabf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 'Marry', '王五', '李小鹏']"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daf237df",
   "metadata": {},
   "source": [
    "#### 1.1.4 删除remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "b15f13d6",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.remove)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dad7cfe8",
   "metadata": {},
   "source": [
    "* 思考：我怎么知道李小鹏在数据列表里面呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "2d9876dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.remove('李小鹏')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "a52d90d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 'Marry', '王五']"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "048050e1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
